package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.repository.StrategyCommentRepositoey;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentRepositoey strategyCommentRepositoey;
    @Autowired
    private MongoTemplate template;
    private Pageable pageable;

    @Override
    public List<StrategyComment> list() {
        return strategyCommentRepositoey.findAll();
    }

    @Override
    public StrategyComment selectOne(String id) {
        return strategyCommentRepositoey.findById(id).orElse(null);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        //创建查询对象
        Query query = new Query();
        //根据攻略id查询
        if(StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        //页面显示集合
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "_id");
        Page<StrategyComment> page = DBHelper.query(template, StrategyComment.class, query, pageable);
        return page;
    }

    @Override
    public void save(StrategyComment strategyComment) {
        strategyCommentRepositoey.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepositoey.deleteById(id);
    }

    @Override
    public void addComment(StrategyComment strategyComment) {
        //添加操作时,要先设置id为null
        strategyComment.setId(null);
        //设置创建时间
        strategyComment.setCreateTime(new Date());
        this.save(strategyComment);
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //点赞
        //根据评论id获取评论对象
        StrategyComment strategyComment = this.selectOne(cid);
        //获取对象中点赞的集合
        List<String> thumbuplist = strategyComment.getThumbuplist();
        //判断用户id是否在集合中
        if(thumbuplist.contains(uid)){
            //如果在集合中,说明已经点过赞,此时执行取消赞操作,将赞减1并将uid移除集合
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            thumbuplist.remove(uid);
        }else{
            //如果不在集合中,说明还没点过赞,此时要执行点赞操作,将赞加1并将uid添加进集合中
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            thumbuplist.add(uid);
        }
        //最后,执行更新操作
        this.save(strategyComment);

    }

   /* @Override
    public StrategyComment queryBySid(String sid) {
        //根据攻略id查询对应的评论信息
        if(StringUtils.hasLength(sid)){
            return strategyCommentRepositoey.findByStrategyId(sid);
        }
        return null;
    }*/

}

